package leetcode.editor.cn.q1_300.q;

import java.util.Stack;

/**
 * @author qidi
 * @date 2021-11-01 17:38:02
 */
class Q0020_ValidParentheses {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isValid(String s) {
            if (s.length() % 2 == 1) return false;
            Stack<Character> stack = new Stack<>();
            char cur;
            for (int i = 0; i < s.length(); i++) {
                cur = s.charAt(i);
                if ((cur == ')' || cur == ']' || cur == '}') && stack.isEmpty()) {
                    return false;
                }
                if (cur == ')' && stack.peek() != '(' || cur == ']' && stack.peek() != '[' || cur == '}' && stack.peek() != '{') {
                    return false;
                }
                if (cur == '(' || cur == '[' || cur == '{') {
                    stack.push(cur);
                } else {
                    stack.pop();
                }
            }
            return stack.isEmpty();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0020_ValidParentheses().new Solution();
        System.out.println(solution.isValid("){"));
        System.out.println(solution.isValid("()[]{}"));
        System.out.println(solution.isValid("(]"));
        System.out.println(solution.isValid("([)]"));
        System.out.println(solution.isValid("([])"));
    }
}